﻿
using System;

namespace Moqikaka.BLL
{
    using Moqikaka.DAL;
    using Moqikaka.Model;
    using Moqikaka.Util.Attr;
    using System.Collections.Concurrent;
    using System.Collections.Generic;
    using System.Data;

    /// <summary>
    /// 玩家回归
    /// </summary>
    [BLLAttribute("PlayerReturn")]
    public class PlayerReturnBLL
    {


        /// <summary>
        /// 获取玩家是否能参加回归活动
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="partnerId"></param>
        /// <returns></returns>
        [BLLActionAttribute("PlayerReturnCheck")]
        public static ResultResponse PlayerReturnCheck(string userId, int partnerId)
        {
            var result = new ResultResponse();
            var cache = PlayerReturnCache.Get(userId, partnerId);
            if (cache == null)
            {
                LoginBLL.RePlayerReturnCheck(userId, partnerId);
            }

            if (!cache.CanJoinPlayerReturn)
            {
                result.ResultCode = ResultCodeEnum.CanNotJoinPlayerReturn;
            }
            else
            {
                if (cache.JoinedPlayerReturn)
                    result.ResultCode = ResultCodeEnum.HasBeenJoinPlayerReturn;
                else
                    result.ResultCode = ResultCodeEnum.CanJoinPlayerReturn;
            }

            return result;
        }


        /// <summary>
        /// 设置已近参加了该活动
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="partnerId"></param>
        /// <returns></returns>
        [BLLActionAttribute("SetPlayerReturnComplete")]
        public static ResultResponse SetPlayerReturnComplete(string userId, int partnerId)
        {
            var result = new ResultResponse();
            var cache = PlayerReturnCache.Get(userId, partnerId);
            if (cache == null)
            {
                LoginBLL.RePlayerReturnCheck(userId, partnerId);
            }

            if (cache.CanJoinPlayerReturn)
            {
                if (cache.JoinedPlayerReturn)
                    //已经参加
                    result.ResultCode = ResultCodeEnum.HasBeenJoinPlayerReturn;
                else
                {
                    PlayerReturn.SetPlayerReturn(userId, partnerId, cache.PlayerReturnLinkLoginTime, true);

                    cache.JoinedPlayerReturn = true;
                    PlayerReturnCache.Set(userId, partnerId,cache);

                    result.ResultCode = ResultCodeEnum.SetPlayerReturnSoccess;
                }
            }
            else
            {
                result.ResultCode = ResultCodeEnum.CanNotJoinPlayerReturn;
            }

            return result;
        }
    }
}
